package com.hake.core.support.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hake.core.exception.AppException;
import com.hake.core.support.CommonDataThreadLocal;
import com.hake.core.support.mapper.BaseMapper;

public abstract class BaseService<T> implements IService<T> {
	
	private Logger logger = LoggerFactory.getLogger(getClass()) ;
	
	@Autowired
	private BaseMapper<T> baseMapper ;
	
	@Override
	public int insert(T entity) throws AppException {
		try {
			return this.baseMapper.insert(entity);
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("插入记录异常") ;
		}
	}

	@Override
	public int insertSelective(T entity) throws AppException {
		try {
			return this.baseMapper.insertSelective(entity);
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("插入记录异常") ;
		}
	}

	@Override
	public int deleteByPrimaryKey(Object id) throws AppException {
		try {
			return this.baseMapper.deleteByPrimaryKey(id);
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("删除记录异常") ;
		}
	}

	@Override
	public int updateByPrimaryKeySelective(T entity) throws AppException {
		try {
			return this.baseMapper.updateByPrimaryKeySelective(entity);
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("修改记录异常") ;
		}
	}

	@Override
	public int updateByPrimaryKey(T entity) throws AppException {
		try {
			return this.baseMapper.updateByPrimaryKey(entity);
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("修改记录异常") ;
		}
	}

	@Override
	public T selectByPrimaryKey(Object id) throws AppException {
		try {
			return this.baseMapper.selectByPrimaryKey(id);
		} catch (RuntimeException e) {
			logger.error("异常：" +e.getMessage());
			throw new AppException("根据ID查询记录异常") ;
		}
	}

	@Override
	public List<T> selectAll() throws AppException {
		try {
			return this.baseMapper.selectAll();
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("查询所有记录异常") ;
		}
	}

	@Override
	public int existsFileldValue(Object id, String field, String fieldVal) throws AppException {
		try {
			return this.baseMapper.existsFileldValue(id, field, fieldVal);
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("检查字段的值是否已存在异常") ;
		}
	}

	@Override
	public int deleteBatchByKeys(Object[] ids) throws AppException {
		try {
			return this.baseMapper.deleteBatchByKeys(ids) ;
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("批量删除记录异常") ;
		}
	}
	
	@Override
	public List<T> selectByPageList(T entity) throws AppException {
		try {
			PageHelper.startPage(CommonDataThreadLocal.getPageParams()) ;
			return this.baseMapper.selectByPage(entity);
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("分页查询异常") ;
		}
	}

	@Override
	public PageInfo<T> selectByPageInfo(T entity) throws AppException {
		try {
			PageHelper.startPage(CommonDataThreadLocal.getPageParams()) ;
			List<T> list = this.baseMapper.selectByPage(entity);
			
			return new PageInfo<T>(list) ;
			
		} catch (RuntimeException e) {
			logger.error(e.getMessage());
			throw new AppException("分页查询异常") ;
		}
	}

	
	
}
